<?php

namespace App\Kuafu\Order\Command;

use App\Kuafu\Base\Command\Traits\KfGetTimeInterval;
use App\Kuafu\Log\Model\UserLoginLogModel;
use App\Kuafu\Order\Model\OrderCouponSplitModel;
use App\Kuafu\Order\Model\OrderDetailSplitModel;
use App\Kuafu\Order\Model\OrderDiscountSplitModel;
use App\Kuafu\Order\Model\OrderPayUnionSplitModel;
use App\Kuafu\Order\Model\OrderSplitModel;
use App\Kuafu\Pay\Model\PayStreamMappingModel;
use App\Kuafu\Pay\Model\PayStreamModel;
use App\Library\FeiShu\SendFs;
use Illuminate\Console\Command;

class OrderStreetSweeperTester extends Command
{
    use KfGetTimeInterval;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order:street-sweeper-tester';

    /**
     * The console command description.
     *
     * @var string
     */

    protected $description = '夸父堂食订单清道夫 - 删除自动化测试数据';


    /**
     * Execute the console command
     *
     * @return int
     */
    public function handle()
    {
        // 删除 一周以前的 自动化测试数据
        $delAfterDays = 7 * 86400;
        $i            = 13315; // 自动化测试店铺 ID - 勿动 否则会删除其他正式店铺数据!!!!

        // 删除数据
        $queryTime = time() - $delAfterDays;

        // 主表数据
        OrderSplitModel::suffix($i)
                       ->query()
                       ->where('shopId', $i)
                       ->withoutGlobalScope('isDel')
                       ->where('createdAt', '<', $queryTime)
                       ->where('createdAt', '>', $queryTime - 86400 * 2)
                       ->delete();

        // 订单详情
        OrderDetailSplitModel::suffix($i)
                             ->query()
                             ->where('shopId', $i)
                             ->withoutGlobalScope('isDel')
                             ->where('createdAt', '<', $queryTime)
                             ->where('createdAt', '>', $queryTime - 86400 * 2)

                             ->delete();
        // 优惠券信息
        OrderCouponSplitModel::suffix($i)
                             ->query()
                             ->where('shopId', $i)
                             ->withoutGlobalScope('isDel')
                             ->where('createdAt', '<', $queryTime)
                             ->where('createdAt', '>', $queryTime - 86400 * 2)
                             ->delete();

        // 整单优惠
        OrderDiscountSplitModel::suffix($i)
                               ->query()
                               ->where('shopId', $i)
                               ->withoutGlobalScope('isDel')
                               ->where('createdAt', '<', $queryTime)
                               ->where('createdAt', '>', $queryTime - 86400 * 2)
                               ->delete();

        // kf_order_pay_union_015 数据删除
        OrderPayUnionSplitModel::suffix($i)
                               ->query()
                               ->where('shopId', $i)
                               ->withoutGlobalScope('isDel')
                               ->where('createdAt', '<', $queryTime)
                               ->where('createdAt', '>', $queryTime - 86400 * 2)
                               ->delete();

        // pay_steam 表数据删除
        PayStreamModel::suffix($i)
                      ->where('shopId', $i)
                      ->where('createdAt', '<', $queryTime)
                      ->where('createdAt', '>', $queryTime - 86400 * 2)
                      ->delete();

        // pay_stream_mappings 表数据删除
        PayStreamMappingModel::query()
                             ->where('shopId', $i)
                             ->where('createdAt', '<', $queryTime)
                             ->where('createdAt', '>', $queryTime - 86400 * 2)
                             ->delete();

        // user_login_logs 表数据删除
        UserLoginLogModel::query()
                         ->where('userId', 51)
                         ->where('ip', 794984250)
                         ->where('createdAt', '<', $queryTime)
                         ->where('createdAt', '>', $queryTime - 86400 * 2)
                         ->delete();

        SendFs::newRobot(SendFs::ROBOT_BUSINESS_WARING)->setTitleContent('[' . date('Y-m-d H:i:s') . '] : ' . '堂食清道夫', '自动化测试门店订单已删除')->send();

        return 0;
    }


}
